<?php
require_once ROOT_ADMIN_MODELS.DS.'Models_Base.php';
class Models_News extends Models_Base{		    
    public function __construct(){
    	parent::__construct();
    	$this->_key="id";
    	$this->_table="news";
    }
 	public function listNews($typenews, $forum, $status, $author,$from,$to)
    {
       	$sql="SELECT 
       				n.id,n.headLines,n.summary,n.`status`,n.count,n.dateNews,u.`name` as author,t.`name` AS typenews,f.`name` AS forum 
       			FROM 
       				news n, `user` u, typenews t,forum f 
       			WHERE 
       				n.author = u.id 
       			AND 
       				n.typeNews_id = t.id 
       			AND 
       				t.forum_id = f.id ";
       	if($typenews != "")
       		$sql.=" AND t.id = $typenews ";
       	if($forum != "")
       		$sql.=" AND f.id = $forum ";
       	if($status != "all")
       		$sql.=" AND n.`status` = $status ";
       	if($author != "")
       		$sql.=" AND u.id = $author ";
       	if($from != "")
       		$sql.=" AND n.dateNews >= $from ";
       	if($to != "")
       		$sql.=" AND n.dateNews <= $to ";
       	
        $data=$this->_db->fetchAll($sql,null,Zend_Db::FETCH_OBJ);
        return $data;
    }
    
    public function NewsByid($id)
    {
    	$sql="SELECT
		    		n.id,n.headLines,n.summary,n.content,n.`status`,n.count,n.dateNews,u.`name` as author,t.`name` AS typenews,f.`name` AS forum
		    	FROM
		    		news n, `user` u, typenews t,forum f
		    	WHERE
		    		n.author = u.id
		    	AND
		    		n.typeNews_id = t.id
		    	AND
		    		t.forum_id = f.id 
    			AND
    				n.id= $id";
    
   		$data=$this->_db->fetchRow($sql,null,Zend_Db::FETCH_OBJ);
    	return $data;
    }
    
    public function addGroup($obj)
    {
    	parent::_insert($obj);
    }
    
    public function editGroup($id)
    {
    	parent::_update($id);
    }
    
    public function delete($id)
    {
    	//delete comment
    	$sql="	DELETE FROM
		    		`comment`
		    	WHERE
		    		news_id=$id";
    	$this->_db->query($sql);
    	//delete quest
    	$sql="	DELETE FROM
		    		`quest_news`
		    	WHERE
		    		news_id=$id";
    	$this->_db->query($sql);
    	//delete news
    	$sql="	DELETE FROM
    				`news`
    			WHERE
    				id=$id";
    	$this->_db->query($sql);
    }
    public function active($id,$status)
    {
    	$sql="	UPDATE
		    		`news`
		    	SET
		    		status=$status
		    	WHERE
		    		id=$id";
    	$this->_db->query($sql);
    }
}
		